﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ApplLogic.ApplClasses;

namespace TCPClient.UI.LogicForUserControls
{
    public enum eMoveType
    {
        Move,
        Swap
    }

    public class MoveWorkerBetweenShiftsHandler
    {
        private DateTime m_StartTimeOfSourceShift;
        private DateTime m_EndTimeOfSourceShift;
        private DateTime m_StartTimeOfDestShift;
        private DateTime m_EndTimeOfDestShift;
        private ApplWorker m_WorkerToRemoveFirst;
        private ApplWorker m_WorkerToRemoveSecond;
        private eMoveType m_MoveType;
        private bool m_IsGotworker;
        private string m_FinishMessage;


        public MoveWorkerBetweenShiftsHandler()
        {
            m_MoveType = eMoveType.Move;
            zeroizeAll();
        }

        public bool IsGotWorker
        {
            get { return m_IsGotworker; }
        }

        public string FinishMessage
        {
            get { return m_FinishMessage; }
        }

        public DateTime StartTimeOfSourceShift
        {
            get { return m_StartTimeOfSourceShift; }
        }

        public DateTime EndTimeOfSourceShift
        {
            get { return m_EndTimeOfSourceShift; }
        }

        public DateTime StartTimeOfDestShift
        {
            get { return m_StartTimeOfDestShift; }
        }

        public DateTime EndTimeOfDestShift
        {
            get { return m_EndTimeOfDestShift; }
        }

        public eMoveType MoveType
        {
            get { return m_MoveType; }
            set { m_MoveType = value; }
        }

        public void SetWorkerAndDates(ApplWorker workerToRemove, DateTime startDate, DateTime endDate)
        {
            if (!m_IsGotworker)
            {
                m_WorkerToRemoveFirst = workerToRemove;
                m_StartTimeOfSourceShift = startDate;
                m_EndTimeOfSourceShift = endDate;
                m_IsGotworker = true;
            }
            else
            {
                m_StartTimeOfDestShift = startDate;
                m_EndTimeOfDestShift = endDate;
                switch (m_MoveType)
                {
                    case eMoveType.Move:
                        sendRequestToServer();
                    break;
                    case eMoveType.Swap:
                        m_WorkerToRemoveSecond = workerToRemove;
                        sendRequestToServer();
                    break;
                }
                zeroizeAll();
            }
        }

        private void zeroizeAll()
        {
            m_IsGotworker = false;
            m_WorkerToRemoveFirst = null;
            m_WorkerToRemoveSecond = null;
        }

        private void sendRequestToServer()
        {
            if (m_MoveType == eMoveType.Move)
            {
                dealWithMove();
            }
            else
            {
                dealWithSwap();
            }
        }

        private void dealWithSwap()
        {
            string messageFirst =
                ScMainUserControlLogicManager.Instance.IsWorkerGotShift(m_WorkerToRemoveFirst.PersonalDetails.Id,
                                                                        m_StartTimeOfDestShift, m_EndTimeOfDestShift);
            string messageSecond = ScMainUserControlLogicManager.Instance.IsWorkerGotShift(m_WorkerToRemoveSecond.PersonalDetails.Id,
                                                                        m_StartTimeOfSourceShift, m_EndTimeOfSourceShift);
             if(messageFirst.Equals("Not Got") && messageSecond.Equals("Not Got"))
             {
                 m_FinishMessage = "Ok";
                 ScMainUserControlLogicManager.Instance.ChangeResultRegistrationOfAworker(
                m_WorkerToRemoveFirst.PersonalDetails.Id,
                m_StartTimeOfSourceShift, m_EndTimeOfSourceShift, m_StartTimeOfDestShift, m_EndTimeOfDestShift);
                 ScMainUserControlLogicManager.Instance.ChangeResultRegistrationOfAworker(
                     m_WorkerToRemoveSecond.PersonalDetails.Id,
                     m_StartTimeOfDestShift, m_EndTimeOfDestShift, m_StartTimeOfSourceShift, m_EndTimeOfSourceShift);

             }
             else if (messageFirst.Equals("Not Got") && !messageSecond.Equals("Not Got"))
             {
                 m_FinishMessage = string.Format("{0} is allready registered to the shift",
                             m_WorkerToRemoveSecond.PersonalDetails.FullName);
             }
             else if (!messageFirst.Equals("Not Got") && messageSecond.Equals("Not Got"))
             {
                 m_FinishMessage = string.Format("{0} is allready registered to the shift",
                      m_WorkerToRemoveFirst.PersonalDetails.FullName);
             }
             else // both failed
             {
                 m_FinishMessage = string.Format("{0} is allready registered to the shift",
                            m_WorkerToRemoveFirst.PersonalDetails.FullName);
                 m_FinishMessage += string.Format("\n{0} is allready registered to the shift",
                    m_WorkerToRemoveSecond.PersonalDetails.FullName);
             }
        }

        private void dealWithMove()
        {
            m_FinishMessage = ScMainUserControlLogicManager.Instance.ChangeResultRegistrationOfAworker(
                m_WorkerToRemoveFirst.PersonalDetails.Id,
                m_StartTimeOfSourceShift, m_EndTimeOfSourceShift, m_StartTimeOfDestShift, m_EndTimeOfDestShift);
            if (!m_FinishMessage.Equals("Ok"))
            {
                m_FinishMessage = string.Format("{0} is allready registered to the shift",
                                                m_WorkerToRemoveFirst.PersonalDetails.FullName);
            }
        }




    }
}